<?php
/*
 * @Description: file content
 * @FilePath: \auxface_certificate\app\certificate\controller\admin\v1\file\Index.php
 */

declare(strict_types=1);

namespace app\certificate\controller\admin\v1\file;

use think\facade\Db;
use think\Request;

class Index
{
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function getList(Request $request)
    {
        $data  = $request->data;
        $limit =  $data['limit'];
        $page =  $data['page'];

        $where = [
            ['f.projectCode', '=', $request->prjCode],
            ['f.isDelete', '=', 0]
        ];

        if (!empty($data['name'])) array_push($where, ['f.name', 'like', $data['name'] . "%"]);
        if (!empty($data['corpCode'])) array_push($where, ['f.corpCode', 'like', $data['corpCode'] . "%"]);

        $list = DB::table('certificate_file')->alias('f')
            ->where($where)
            ->field(['f.name', 'f.remark', 'f.inDate', 'f.uuid', 'f.file','f.fileType','f.corpCode','f.subtitle','f.fileName','f.expireTime'])
            ->order('f.id desc')
            ->page((int)$page, (int)$limit)
            ->select()
            ->toArray();

        $count = DB::table('certificate_file')->alias('f')
            ->where($where)
            ->count();

        $data = compact('list', 'count');

        return result(SUCCESS, $data);
    }
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function create(Request $request)
    {
        $data = $request->data;

        $insert =  [
            'uuid' => createUuid(),
            'projectCode' => $request->prjCode,
            'corpCode' => $data['corpCode'],
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
            'fileType' => $data['fileType'] ?? '',
            'file' => $data['file'] ?? '',
            'formData' =>  json_encode($data['formData'] ?? [], JSON_UNESCAPED_UNICODE),
            'subtitle' => $data['subtitle'],
            'effectTime' => $data['effectTime'] ?? '',
            'expireTime' => $data['expireTime'] ?? '',
            'fileName' => basename($data['file'] ?? '') ?: ''
        ];
        DB::table('certificate_file')->insert($insert);

        return result(SUCCESS, ['uuid' => $insert['uuid'], 'inDate' => date('Y-m-d H:i:s')], '添加成功!');
    }

    /**
     * @Description: 修改
     */
    public function update(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $cehck = DB::table('certificate_file')->where([
            ['uuid', '<>', $uuid],
            ['name', '=', $data['name']],
            ['isDelete', '=', 0],
        ])->value('id');
        if ($cehck) return result(FAIL, '', '名称已存在,无法修改!');
        $updateData = [
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
            'fileType' => $data['fileType'] ?? '',
            'file' => $data['file'] ?? '',
            'formData' =>  json_encode($data['formData'] ?? [], JSON_UNESCAPED_UNICODE),
            'subtitle' => $data['subtitle'],
            'corpCode' => $data['corpCode'],
            'effectTime' => $data['effectTime'] ?? '',
            'expireTime' => $data['expireTime'] ?? '',
            'fileName' => basename($data['file'] ?? '') ?: ''
        ];
        DB::table('certificate_file')->where('uuid', $uuid)->update($updateData);
        return result(SUCCESS, '', '更改成功');
    }

    public function destroy(Request $request)
    {
        $data = $request->data;
        DB::table('certificate_file')->where('uuid', $data['uuid'])->update(['isDelete' => 1]);

        //删除关联数据
        DB::table('certificate_fileTypeLink')->where('fileUuid', $data['uuid'])->delete();
        DB::table('certificate_digitalMachineFile')->where('fileUuid', $data['uuid'])->delete();
        return result(SUCCESS, '', '删除成功');
    }

    /**
     * @Description: 获取数据
     * @param {Request} $request
     */
    public function getDetail(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $data = DB::table('certificate_file')->where('uuid', $uuid)->field(['inDate', 'corpCode', 'name', 'remark', 'uuid','fileType','file','formData', 'subtitle','effectTime','expireTime','fileName'])->find();
        $data['formData'] = json_decode($data['formData'], true);
        return result(SUCCESS, $data, '');
    }

}
